package zhoufu.sz.sanxiao_check.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;

public class BeanUtils {
    /**
     * 把javabean转换成 map ,  必须是使用了  @Convert 这个注解的才会转换
     *
     * @param obj
     * @return
     */
    public static HashMap<String, String> beanToMap(Object obj) {
        HashMap<String, String> map = new HashMap<String, String>();
        try {
            Field[] fields = obj.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                String name = field.getName();
                Object value = field.get(obj);

                value = (value==null|| TextUtils.isEmpty(value.toString()))? "" : value.toString();
                if (!name.equals("$change"))
                map.put(name, value.toString());

            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 复制 bean
     *
     * @param src
     * @param desc
     */
    public static void copy(Object src, Object desc) {
        try {
            Field[] fields = desc.getClass().getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                Field field = src.getClass().getDeclaredField(fields[i].getName());
                fields[i].setAccessible(true);
                fields[i].set(desc, field.get(src));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}